<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* SELECTRESOURCES class
*
* Select database resources on a variety of parameters
*****/
class SELECTRESOURCES
{
// Constructor
	function SELECTRESOURCES($db, $vars)
	{
		$this->db = $db;
		$this->vars = $vars;
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/messages/ERRORS.php");
		$this->errors = new ERRORS();
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/list/LISTCOMMON.php");
		$this->common = new LISTCOMMON($this->db, $this->vars);
		include_once("core/html/MISC.php");
		include_once("core/html/FORM.php");
	}
// display form options
	function display($error = FALSE)
	{
//		$this->session->delVar('mywikindx_pagingStart');
		$this->template->setVar('heading', $this->messages->text("heading", "select"));
///First check, do we have resources?
		$recordset = $this->db->select(array('WKX_database_summary'), array('totalResources'));
		if(!$this->db->fetchOne($recordset))
		{
			$this->template->setVar('body', $this->messages->text('misc', 'noResources'));
			return $this->template->process();
		}
		$this->userBib = $this->session->getVar("mywikindx_bibliography_use");
		$bib = FALSE;
		if($this->userBib)
		{
			$recordset = $this->db->select(array('WKX_user_bibliography'), array('title'), 
				" WHERE " . $this->db->formatfield('id') . "=" . $this->db->tidyInput($this->userBib));
			$row = $this->db->fetchRow($recordset);
			$bib = MISC::p(MISC::span("(" . $this->messages->text("user", "bibliography") . ": " . 
				$row['title'] . ")", "hint"));
		}
		else if($this->session->getVar('setup_multiUser'))
			$bib = MISC::p(MISC::span("(" . $this->messages->text("user", "bibliography") . ": " . 
			$this->messages->text("user", "masterBib") . ")", "hint"));
		include_once("core/html/MISC.php");
		include_once("core/html/TABLE.php");
		$pString = $error ? $error : FALSE;
		$pString .= $bib;
		$pString .= FORM::formHeader("selectProcess");
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$this->grabTypes();
		$pString .= TABLE::td($this->makeFormMultiple("type", $this->type));
// There may not yet be tags in the database.
		$this->grabTags();
		if($this->tag)
			$pString .= TABLE::td($this->makeFormMultiple("tag", $this->tag));
		else
			$pString .= TABLE::td("&nbsp;");
		include_once("core/user/USER.php");
		$user = new USER($this->db);
		$this->users = $user->grabAll(TRUE);
		$pString .= TABLE::td($this->makeFormMultiple("addedBy", $this->users));
		$pString .= TABLE::td($this->makeFormMultiple("editedBy", $this->users));
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= TABLE::tdEnd() . TABLE::trEnd() . TABLE::trStart() . TABLE::tdStart() . MISC::hr();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$this->grabGroups();
		$pString .= TABLE::td($this->makeFormMultiple("category", $this->group));
// There may not yet be keywords in the database.
		$this->grabKeywords();
		if($this->keyword)
			$pString .= TABLE::td($this->makeFormMultiple("keyword", $this->keyword));
		else
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::td($this->userBibs());
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= TABLE::tdEnd() . TABLE::trEnd() . TABLE::trStart() . TABLE::tdStart() . MISC::hr();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$this->grabCreators();
		if($this->creator)
			$pString .= TABLE::td($this->makeFormMultiple("creator", $this->creator));
		else
			$pString .= TABLE::td("&nbsp;");
		$this->grabPublishers();
		if($this->publisher)
			$pString .= TABLE::td($this->makeFormMultiple("publisher", $this->publisher));
		else
			$pString .= TABLE::td("&nbsp;");
		$this->grabCollections();
		if($this->collection)
			$pString .= TABLE::td($this->makeFormMultiple("collection", $this->collection, 60));
		else
			$pString .= TABLE::td("&nbsp;");
		$pString .= TABLE::trEnd();
		$pString .= TABLE::tableEnd();
		$pString .= MISC::hr();
		$pString .= TABLE::tableStart();
		$pString .= TABLE::trStart();
		$pString .= TABLE::td($this->common->displayOrder('select'));
		$checked = $this->session->getVar('select_attachment') ? TRUE : FALSE;
		$selectAttachmentString = MISC::p($this->messages->text('select', 'attachment') .  ":&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, 'select_attachment', $checked), FALSE, FALSE, 'bottom');
		$checked = $this->session->getVar('select_displayAttachment') ? TRUE : FALSE;
		$displayAttachmentString = MISC::p("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . 
			$this->messages->text('select', 'displayAttachment') . ":&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, 'select_displayAttachment', $checked));
		if(file_exists("/usr/bin/gzip") && file_exists("/bin/tar"))
		{
			$checked = $this->session->getVar('select_displayAttachmentZip') ? TRUE : FALSE;
			$zipString = MISC::p("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . 
				$this->messages->text('select', 'displayAttachmentZip') . ":&nbsp;&nbsp;" . 
				FORM::checkbox(FALSE, 'select_displayAttachmentZip', $checked) . $zipString);
		}
		else
			$zipString = FALSE;
		$checked = $this->session->getVar('select_displayPeerReviewed') ? TRUE : FALSE;
		$peerReviewedString = MISC::p($this->messages->text('select', 'displayPeerReviewed') .  ":&nbsp;&nbsp;" . 
			FORM::checkbox(FALSE, 'select_displayPeerReviewed', $checked), FALSE, FALSE, 'bottom');
		$pString .= TABLE::td($selectAttachmentString . $displayAttachmentString . $zipString . $peerReviewedString);
		if(is_array($this->session->getArray("select")))
		{
			$this->session->clearArray("select");
			$reset = FORM::formHeader("selectDisplay") . 
				FORM::formSubmit("Reset") . FORM::formEnd();
			$pString .= TABLE::td(MISC::p(FORM::formSubmit("Select") . FORM::formEnd() . $reset));
		}
		else
			$pString .= TABLE::td(MISC::br() . FORM::formSubmit("Select") . MISC::br() . MISC::br() . 
				FORM::formReset() . FORM::formEnd());
		$pString .= TABLE::tableEnd();
		$this->template->setVar('body', $pString);
		return $this->template->process();
	}
// Return select box of user bibliographies for this user if browsing the master bibliography
	function userBibs()
	{
		if(!$this->session->getVar("mywikindx_bibliographies"))
			return "&nbsp;";
		$bibs = unserialize($this->session->getVar("mywikindx_bibliographies"));
		if($this->userBib)
			unset($bibs[$this->userBib]);
		if(empty($bibs))
			return "&nbsp;";
// add 0 => IGNORE to $array
		$temp[0] = $this->messages->text("misc", "ignore");
		foreach($bibs as $key => $value)
			$temp[$key] = $value;
		$selected = $this->session->getVar("select_bibId");
		if($selected && array_key_exists($selected, $bibs))
			$pString = FORM::selectedBoxValue($this->messages->text("select", "notInUserBib"),
			"select_bibId", $temp, $selected, 5);
		else
			$pString = FORM::selectFBoxValue($this->messages->text("select", "selectBy", " " . 
			$this->messages->text("select", "notInUserBib")), "select_bibId", $temp, 5);
		return $pString;
	}
// make a self-contained form with a multiple select box
	function makeFormMultiple($type, $array, $width = FALSE)
	{
// add 0 => IGNORE to $array
		if($type != 'type')
		{
			$temp[0] = $this->messages->text("misc", "ignore");
			foreach($array as $key => $value)
				$temp[$key] = $value;
		}
		else
			$temp = $array;
		if($selected = $this->session->getVar("select_" . $type))
		{
			$selectedArray = explode(",", $selected);
			$pString = FORM::selectedBoxValueMultiple($this->messages->text("select", $type), 
				"select_" . $type, $temp, $selectedArray, "5", $width);
		}
		else
		{
			if($type == 'type')	// select all initially
				$pString = FORM::selectedBoxValueMultiple($this->messages->text("select", 'type'), 
					"select_type", $temp, array_keys($array), "5");
			else
				$pString = FORM::selectFBoxValueMultiple($this->messages->text("select", $type), 
					"select_" . $type, $temp, "5", $width);
		}
		$pString .= MISC::br() . $this->messages->text("hint", "multiples") . MISC::br();
		if($this->radioButtons)
			$pString .= $this->radioButtons . MISC::br();
		return $pString;
	}
// get types from database.
	function grabTypes()
	{
		include_once("core/resource/RESOURCEMAP.php");
		$resourceMap = new RESOURCEMAP();
		include_once("core/messages/UTF8.php");
		$this->utf8 = new UTF8();
		foreach($resourceMap->types as $key)
			$this->type[$key] = $this->utf8->encodeUtf8($this->messages->text("resourceType", $key));
		$this->radioButtons = FALSE;
	}
// get tags from database.
	function grabTags()
	{
//		if(!$this->session->getVar("setup_superadmin"))
//		{
//			$this->tag = FALSE;
//			return;
//		}
		include_once("core/file/TAG.php");
		$tag = new TAG($this->db);
		$this->tag = $tag->grabAll();
		$this->radioButtons = FALSE;
	}
// get groups from database.
	function grabGroups()
	{
		include_once("core/html/FORM.php");
		include_once("core/group/GROUP.php");
		$group = new GROUP($this->db);
		$this->group = $group->grabAll();
		$this->makeRadioButtons('category');
	}
// get keywords from database.
	function grabKeywords()
	{
		include_once("core/keyword/KEYWORD.php");
		$keyword = new KEYWORD($this->db);
		$this->keyword = $keyword->grabAll($this->userBib);
		$this->makeRadioButtons('keyword');
	}
// get creators from database.
	function grabCreators()
	{
		include_once("core/html/FORM.php");
		include_once("core/creator/CREATOR.php");
		$creator = new CREATOR($this->db);
		$this->creator = $creator->grabAll($this->userBib);
		$this->makeRadioButtons('creator');
	}
// get publishers from database.
	function grabPublishers()
	{
		include_once("core/html/FORM.php");
		include_once("core/publisher/PUBLISHER.php");
		$publisher = new PUBLISHER($this->db);
		$this->publisher = $publisher->grabAll(FALSE, $this->userBib);
		$this->radioButtons = FALSE;
	}
// get collections from database.
	function grabCollections()
	{
		include_once("core/html/FORM.php");
		include_once("core/collection/COLLECTION.php");
		$collection = new COLLECTION($this->db);
		$this->collection = $collection->grabAll(FALSE, $this->userBib);
		$this->radioButtons = FALSE;
	}
// Create radio buttons for AND and OR
	function makeRadioButtons($type)
	{
		include_once("core/html/FORM.php");
		$type = 'select_' . $type . 'Method';
		if($this->session->getVar($type) == 'AND')
		{
			$pString = MISC::span(FORM::radioButton(FALSE, $type, 'OR') . " OR", "small") . MISC::br();
			$pString .= MISC::span(FORM::radioButton(FALSE, $type, 'AND', TRUE) . " AND", "small");
		}
// Default
		else
		{
			$pString = MISC::span(FORM::radioButton(FALSE, $type, 'OR', TRUE) . " OR", "small") . MISC::br();
			$pString .= MISC::span(FORM::radioButton(FALSE, $type, 'AND') . " AND", "small");
		}
		$this->radioButtons = $pString;
	}
	function process($message = FALSE, $reorder = FALSE)
	{
/*
		if(array_key_exists('bookmark', $this->vars))
		{
			$this->template->setVar('heading', $this->messages->text("heading", "select"));
			$bookmarks = $this->session->getArray("bookmark");
			$listType = base64_decode($this->session->getVar("bookmark_lastMultiType"));
			$sqlTotal = stripslashes(base64_decode($this->session->getVar("bookmark_lastMultiSql")));
			if($listType == 'select')
				$queryString = "action=selectProcess&bookmark=1";
			else // statistics
				$queryString = "action=selectProcess&bookmark=1&statistics=" . $this->vars['statistics'] . 
					'&' . $this->vars['id'];
			list($pString, $pagingString) = $this->common->listResources($sqlTotal, $queryString, $listType, 
				FALSE, $message, $pagingObject);
			$this->template->setVar('body', $pString);
			$this->template->setVar('paging', $pagingString);
			return $this->template->process();
		}
*/
// if $this->vars['statistics'], this is from STATISTICS()
		if(!$reorder && !array_key_exists('statistics', $this->vars))
			$input = $this->checkInput();
		else if(array_key_exists('statistics', $this->vars))
		{
			if($this->vars['action'] != 'lastMulti')
				$this->session->delVar('mywikindx_pagingStart');
			if($this->vars['statistics'] == 'type')
			{
				$input['type'] = $this->vars['id'];
				$this->inputMethod['type'] = 'OR';
			}
			$input['statistics'] = $this->vars['statistics'];
			$this->session->setVar('statistics_type', $this->vars['statistics']);
			$this->session->setVar('statistics_id', $this->vars['id']);
			if(!$reorder) // default ORDER by creator ascending
			{
				$input['order'] = 'creator';
				$input['ascDesc'] = 'ASC';
			}
			else
			{
				if(array_key_exists('select_order', $this->vars) && $this->vars['select_order'])
				{
					$input['order'] = $this->vars['select_order'];
					$this->session->setVar('select_order', $input['order']);
				}
				if(array_key_exists('select_ascDesc', $this->vars) && $this->vars['select_ascDesc'])
				{
					$input['ascDesc'] = $this->vars['select_ascDesc'];
					$this->session->setVar('select_ascDesc', $input['ascDesc']);
				}
			}
		}
		else
		{
			$input = $this->session->getArray("select");
			if(array_key_exists('select_order', $this->vars) && $this->vars['select_order'])
			{
				$input['order'] = $this->vars['select_order'];
				$this->session->setVar('select_order', $input['order']);
			}
			if(array_key_exists('select_ascDesc', $this->vars) && $this->vars['select_ascDesc'])
			{
				$input['ascDesc'] = $this->vars['select_ascDesc'];
				$this->session->setVar('select_ascDesc', $input['ascDesc']);
			}
			$typesArray = array("type", "tag", "category", "keyword", "creator", "publisher", "collection");
			foreach($typesArray as $type)
			{
				$method = $type . "Method";
				if(array_key_exists($method, $input))
					$this->inputMethod[$type] = $input[$method];
			}
			if(array_key_exists('select_attachment', $this->vars))
			{
				$input['select_attachment'] = TRUE;
				$this->session->setVar('select_attachment', TRUE);
			}
		}
		include_once("core/html/MISC.php");
		include_once("core/sql/STATEMENTS.php");
		$stmt = new STATEMENTS($this->db);
		$selectJoin = $stmt->selectJoin();
		$this->template->setVar('heading', $this->messages->text("heading", "select"));
		$tableArray = $stmt->idIsIdTables();
		$condition = array();
		$listType = 'select';
		foreach($input as $type => $value)
		{
			if($type == 'type')
			{
				include_once("core/type/TYPE.php");
				$method = new TYPE($this->db);
				$condition[] = "(" . $method->selectCondition(array('type' => $value, 
					'typeMethod' => $this->inputMethod['type'])) . ")";
			}
			else if($type == 'category')
			{
				include_once("core/group/GROUP.php");
				$method = new GROUP($this->db);
				$condition[] = "(" . $method->selectCondition(array('category' => $value, 
					'categoryMethod' => $this->inputMethod['category'])) . ")";
			}
			else if($type == 'keyword')
			{
				include_once("core/keyword/KEYWORD.php");
				$method = new KEYWORD($this->db);
				$condition[] = "(" . $method->selectCondition(array('keyword' => $value, 
					'keywordMethod' => $this->inputMethod['keyword'])) . ")";
			}
			else if($type == 'tag')
			{
				include_once("core/file/TAG.php");
				$method = new TAG($this->db);
				$condition[] = "(" . $method->selectCondition(array('tag' => $value, 
					'tagMethod' => $this->inputMethod['tag'])) . ")";
			}
			else if($type == 'creator')
			{
				include_once("core/creator/CREATOR.php");
				$method = new CREATOR($this->db);
				$condition[] = "(" . $method->selectCondition(array('creator' => $value, 
					'creatorMethod' => $this->inputMethod['creator'])) . ")";
			}
			else if($type == 'publisher')
			{
				include_once("core/publisher/PUBLISHER.php");
				$method = new PUBLISHER($this->db, $this->vars, $input);
				$condition[] = "(" . $method->selectCondition(array('publisher' => $value, 
					'publisherMethod' => $this->inputMethod['publisher'])) . ")";
			}
			else if($type == 'collection')
			{
				include_once("core/collection/COLLECTION.php");
				$method = new COLLECTION($this->db, $this->vars, $input);
				$condition[] = "(" . $method->selectCondition(array('collection' => $value, 
					'collectionMethod' => $this->inputMethod['collection'])) . ")";
			}
			else if($type == 'bibId')
			{
				$recordSet = $this->db->select(array('WKX_user_bibliography'), array('bibliography'), 
					" WHERE " . $this->db->formatField('WKX_user_bibliography.id') . "=" . 
					$this->db->tidyInput($input['bibId']));
				if($bibliography = $this->db->fetchOne($recordSet))
				{
					$ids = explode(",", $bibliography);
					$condition[] = $this->db->formatField('WKX_resource.id') ."!=" . 
						join(" AND " . $this->db->formatField('WKX_resource.id') ."!=", $ids);
				}
			}
			else if($type == 'attachment')
			{
				$selectJoin .= "LEFT JOIN " . $this->db->formatTable('WKX_attachments') . " ON " . 
					$this->db->formatfield("WKX_resource.id") . "=" . 
					$this->db->formatfield("WKX_attachments.resourceId");
				$condition[] = "(" . $this->db->formatField('WKX_attachments.resourceId') . ")";
			}
			else if($type == 'addedBy')
			{
				foreach(split(',', $value) as $in)
				{
					if(!$in) // IGNORE
						continue;
					$tempCond[] = $this->db->formatField('addUserIdResource') . '=' . $this->db->tidyInput($in);
				}
				$condition[] = ' (' . join(' OR ', $tempCond) . ') ';
				unset($tempCond);
			}
			else if($type == 'editedBy')
			{
				foreach(split(',', $value) as $in)
				{
					if(!$in) // IGNORE
						continue;
					$tempCond[] = $this->db->formatField('editUserIdResource') . '=' . $this->db->tidyInput($in);
				}
				$condition[] = ' (' . join(' OR ', $tempCond) . ') ';
				unset($tempCond);
			}
			else if($type == 'displayPeerReviewed')
				$condition[] = "(" . $this->db->formatField('peerReviewed') . '=' . $this->db->tidyInput('Y') . ")";
			else if($type == 'statistics')
			{
				$listType = 'statistics';
				if($value == 'resourceUser')
				{
					$condition[] = $this->db->formatField('addUserIdResource') . "=" . 
						$this->db->tidyInput($this->vars['id']);
				}
				else if($value == 'quoteUser')
				{
					$selectJoin .= "LEFT JOIN " . $this->db->formatTable('WKX_resource_quote') . " ON " . 
						$this->db->formatfield("WKX_resource.id") . "=" . 
						$this->db->formatfield("WKX_resource_quote.resourceId");
					$selectJoin .= "LEFT JOIN " . $this->db->formatTable('WKX_resource_quote_text') . " ON " . 
						$this->db->formatfield("WKX_resource_quote.id") . "=" . 
						$this->db->formatfield("WKX_resource_quote_text.id");
					$condition[] = $this->db->formatField('addUserIdQuote') . "=" . 
						$this->db->tidyInput($this->vars['id']);
				}
				else if($value == 'paraphraseUser')
				{
					$selectJoin .= "LEFT JOIN " . $this->db->formatTable('WKX_resource_paraphrase') . " ON " . 
						$this->db->formatfield("WKX_resource.id") . "=" . 
						$this->db->formatfield("WKX_resource_paraphrase.resourceId");
					$selectJoin .= "LEFT JOIN " . 
						$this->db->formatTable('WKX_resource_paraphrase_text') . " ON " . 
						$this->db->formatfield("WKX_resource_paraphrase.id") . "=" . 
						$this->db->formatfield("WKX_resource_paraphrase_text.id");
					$condition[] = $this->db->formatField('addUserIdParaphrase') . "=" . 
						$this->db->tidyInput($this->vars['id']);
				}
				else if($value == 'musingUser')
				{
					$selectJoin .= "LEFT JOIN " . $this->db->formatTable('WKX_resource_musing') . " ON " . 
						$this->db->formatfield("WKX_resource.id") . "=" . 
						$this->db->formatfield("WKX_resource_musing.resourceId");
					$selectJoin .= "LEFT JOIN " . 
						$this->db->formatTable('WKX_resource_musing_text') . " ON " . 
						$this->db->formatfield("WKX_resource_musing.id") . "=" . 
						$this->db->formatfield("WKX_resource_musing_text.id");
					$condition[] = $this->db->formatField('addUserIdMusing') . "=" . 
						$this->db->tidyInput($this->vars['id']);
				}
			}
//			default:
//				$this->badInput($this->errors->text("inputError", "missing", " (select_method) "));
		}
		if(!empty($condition))
			$condition = join(" AND ", $condition);
		else
			$condition = FALSE;
		if($userBibCondition = $this->common->userBibCondition())
			$userBibCondition = $condition ? " AND " . $userBibCondition : " WHERE " . $userBibCondition;
		$condition = $condition ? " WHERE $condition" . $userBibCondition : $userBibCondition;		
// Parse ordering
		$condition .= $this->common->parseOrder($input);
		$sqlTotal = $this->db->selectNoExecute(array("WKX_resource"),  
			$stmt->listFields($input['order']), $selectJoin . " " . $condition);
		$sqlTotal = str_replace("SELECT ", "SELECT DISTINCT ", $sqlTotal);
		if($listType == 'select')
			$queryString = "action=selectProcess";
		else // statistics
			$queryString = "action=selectProcess&statistics=" . $this->vars['statistics'] . 
				"&id=" . $this->vars['id'];
		list($sqlTotal, $pagingObject) = $this->common->pagingStyle($sqlTotal, $input['order'], TRUE);
		list($pString, $pagingString) = $this->common->listResources($sqlTotal, $queryString, $listType, 
			FALSE, $message, $pagingObject);
		$this->template->setVar('body', $pString);
		$this->template->setVar('paging', $pagingString);
		return $this->template->process();
	}
// validate user input
// Input comes either from form input or, when paging, from the session.
	function checkInput()
	{
		$inputFound = FALSE;
		$typesArray = array("type", "tag", "addedBy", "editedBy", "category", "keyword", "creator", "publisher", "collection");
		$temp = array();
		if((array_search('lastMulti', $this->vars) !== FALSE) || array_key_exists('pagingStart', $this->vars))
		{
			foreach($this->session->getArray("select") as $key => $value)
			{
				if(array_search($key, $typesArray) !== FALSE)
					$this->vars['select_' . $key] = explode(',', $value);
				else
					$this->vars['select_' . $key] = $value;
			}
		}
		else
			$this->session->delVar('mywikindx_pagingStart');
// Delete previous stored data
		$this->session->clearArray("select");
		foreach($typesArray as $type)
		{
			$inputKey = 'select_' . $type;
			$method = "select_" . $type . "Method";
			if(!array_key_exists($inputKey, $this->vars))
				continue;
			if((sizeof($this->vars[$inputKey]) == 1) && !$this->vars[$inputKey][0]) // IGNORE
				continue;
			$inputFound = TRUE;
			if(array_key_exists(0, $this->vars[$inputKey]) && !$this->vars[$inputKey][0])
				unset($this->vars[$inputKey][0]);
			$temp[$inputKey] = implode(",", $this->vars[$inputKey]);
// If missing the following, set to default 'OR'.
			if(!array_key_exists($method, $this->vars))
				$this->vars[$method] = 'OR';
			$this->inputMethod[$type] = $this->vars[$method];
			$this->session->setVar($method, $this->vars[$method]);
		}
		if(array_key_exists('select_bibId', $this->vars) && $this->vars['select_bibId'])
		{
			$temp['select_bibId'] = $this->vars['select_bibId'];
			$inputFound = TRUE;
		}
		if(array_key_exists('select_attachment', $this->vars))
		{
			$temp['select_attachment'] = TRUE;
			$inputFound = TRUE;
			if(array_key_exists('select_displayAttachment', $this->vars))
			{
				$temp['select_displayAttachment'] = TRUE;
				if(array_key_exists('select_displayAttachmentZip', $this->vars))
					$temp['select_displayAttachmentZip'] = TRUE;
			}
		}
		if(array_key_exists('select_displayPeerReviewed', $this->vars) && 
			$this->vars['select_displayPeerReviewed'])
		{
			$temp['select_displayPeerReviewed'] = $this->vars['select_displayPeerReviewed'];
			$inputFound = TRUE;
		}
		if(array_key_exists('select_order', $this->vars) && $this->vars['select_order'])
			$temp['select_order'] = $this->vars['select_order'];
		if(array_key_exists('select_ascDesc', $this->vars) && $this->vars['select_ascDesc'])
			$temp['select_ascDesc'] = $this->vars['select_ascDesc'];
		if(!$inputFound || !array_key_exists('select_type', $temp))
			$this->badInput($this->errors->text("inputError", "missing"));
		$this->session->writeArray($temp);
		return $this->session->getArray("select");
	}
// Error handling
	function badInput($error)
	{
		include_once("core/html/CLOSE.php");
		new CLOSE($this->db, $this->display($error));
	}
}
?>
